/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Modelo;

import BaseDatos.Conexion;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author JEANPIERE
 */
public class RegistroDetallesExamenes {
      Connection cn;

    public RegistroDetallesExamenes() {
         Conexion con = new Conexion();
        cn = con.conectar();
    }
    
    public boolean registrar(String codigoExamen,String codigoAlumno,double nota,String fecha){
         boolean resp = false;
        try
        {
            String sql = "INSERT INTO detalleexamen(codigoExamen, codigoAlumno, nota,fecha) VALUES(?,?, ?,?)";
            PreparedStatement cmd = cn.prepareStatement(sql);
            cmd.setString(1, codigoExamen);
            cmd.setString(2, codigoAlumno);
            cmd.setDouble(3, nota);
            cmd.setString(4, fecha);
           
            if(!cmd.execute())
            {
                resp = true;
            }
            cmd.close();
            cn.close();
        }
        catch(Exception ex)
        {
            System.out.println("Error: " + ex.getMessage());
        }
        return resp;
        
        
        
    }
      
    
    public  ArrayList<DetalleExamen>  buscarHistorialAlumno(String codigoAlumno){//vota null si no existe historial e.e
        ArrayList<DetalleExamen> listExamenes= new ArrayList<>(); 
        int cont=0;
        try
        {
            String sql = "SELECT * FROM detalleexamen WHERE codigoAlumno = ?";
            PreparedStatement cmd = cn.prepareStatement(sql);
            cmd.setString(1, codigoAlumno);
            ResultSet rs = cmd.executeQuery();
            while(rs.next())
            {
               DetalleExamen deAux= new DetalleExamen();     
               deAux.setCodigoExamen(rs.getString(2));
               deAux.setCodigoAlumno(rs.getString(3));
               deAux.setNota(rs.getDouble(4));
               deAux.setFecha(rs.getString(5));
                listExamenes.add(deAux);
                cont++;
            }
            cmd.close();
            cn.close();
        }
        catch(Exception ex)
        {
            System.out.println("Error: " + ex.getMessage());
        }
        if(cont!=0)
            return listExamenes;
        else
            return null;
        
    }
    
    public boolean unicidadRegistro(String codigoExamen,String codigoAlumno){
          boolean rep=true;
        try
        {
            String sql = "SELECT * FROM detalleexamen WHERE codigoExamen = ? AND codigoAlumno=?";
            PreparedStatement cmd = cn.prepareStatement(sql);
            cmd.setString(1, codigoExamen);
            cmd.setString(2, codigoAlumno);
            ResultSet rs = cmd.executeQuery();
            if(rs.next())
            {
               rep=false;  
            }
            cmd.close();
            cn.close();
        }
        catch(Exception ex)
        {
            System.out.println("Error: " + ex.getMessage());
        }
        return rep;
        
    }
    
}
